/*
 =============================================================================
 FILE...............: DatabaseService.java
 COMMENTS...........: Represents a database service.
 =============================================================================
 AUTHOR(S)..........: Everton Ranielly de Sousa Cavalcante
 					  Department of Informatics and Applied Mathematics
 					  Federal University of Rio Grande do Norte
 LAST MODIFICATION..: November 26th, 2012
 ============================================================================= 					  
*/


/* ========================================================================= */
/* PACKAGES							                                         */
/* ------------------------------------------------------------------------- */
package services;


/* ========================================================================= */
/* IMPORTS							                                         */
/* ------------------------------------------------------------------------- */
import java.util.ArrayList;
import java.util.List;

import dao.BookingDAO;
import dao.FlightDAO;
import dao.ReceiptDAO;

import model.Booking;
import model.Flight;
import model.Receipt;


/* ========================================================================= */
/* CLASS DatabaseService			                                         */
/* ------------------------------------------------------------------------- */
/**
 * Represents a database service
 * @author Everton Cavalcante
 */
public class DatabaseService 
{
	/**
	 * Searches for available flights considering the user specified criteria
	 * @param from Departure airport
	 * @param to Arrival airport
	 * @param departureDate Departure date
	 * @param arrivalDate Departure time
	 * @return Available flights
	 */
	public Flight[] searchFlights(String from, String to, String departureDate, String arrivalDate) 
	{
		// Creates FlightDAO instance
		FlightDAO flightDAO = new FlightDAO();
		
		// List of available departure flights
		List<Flight> departureFlights = flightDAO.getAvailableFlights(from, to, departureDate);
		
		// List of available return flights
		List<Flight> returnFlights = flightDAO.getAvailableFlights(to, from, arrivalDate);
		
		if (departureFlights.size() == 0 || returnFlights.size() == 0)
			return null;
		else
		{
			// Flights to be returned
			List<Flight> dump = new ArrayList<Flight>(departureFlights.size() + returnFlights.size());
			dump.addAll(departureFlights);
			dump.addAll(returnFlights);
			
			return dump.toArray(new Flight[dump.size()]);
		}
	}
	
	
	/**
	 * Updates database by inserting the generated booking and receipt
	 * @param booking Generated booking
	 * @param receipt Generated receipt
	 * @return A boolean flag that confirms if the operation was successfully
	 * 		   performed (<code>true</code>) or not (<code>false</code>)
	 */
	public boolean updateInformation(Booking booking, Receipt receipt) 
	{	
		BookingDAO bookingDAO = new BookingDAO();		// BookingDAO instance
		bookingDAO.insert(booking);						// Inserts the generated booking in the database
		
		ReceiptDAO receiptDAO = new ReceiptDAO();		// ReceiptDAO instance
		receiptDAO.insert(receipt);						// Inserts the generated receipt in the database
		
		// FIXME How this service would be implemented?
		return true;
	}
}
